import { check, checkError, getRequest } from "./baseUtils"

/**
 * 数据结构
 *   Address
 * 	   id: string
 *     name: string
 * 	   phone: string
 * 	   address: string
 * 	   isDefault: boolean
 */
class AddressApi {
	
	static baseUrl = "/address"
	
	static async getAll() {
		uni.showLoading({title: "加载中", mask: true})
		try {
			const response = await uni.request(getRequest({url: this.baseUrl, method: "GET"}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	// form: {name: string, phone: string, address: string}
	static async create(form) {
		uni.showLoading({title: "创建中", mask: true})
		try {
			const response = await uni.request(getRequest({url: this.baseUrl, method: "POST", data: form}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async deleteById(id) {
		uni.showLoading({title: "删除中", mask: true})
		try {
			const response = await uni.request(getRequest({url: `${this.baseUrl}/${id}`, method: "DELETE"}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async updateById(id, form) {
		uni.showLoading({title: "修改中", mask: true})
		try {
			const response = await uni.request(getRequest({url: `${this.baseUrl}/${id}`, method: "PUT", data: form}))
			return check(response)
		}
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
	
	static async setDefault(id) {
		uni.showLoading({title: "设置中", mask: true})
		try {
			const response = await uni.request(getRequest({url: `${this.baseUrl}/set_default?id=${id}`, method: 'PUT'}))
			return check(response)		
		} 
		catch (error) { checkError(error) }
		finally { uni.hideLoading() }
	}
}

const key = "address"

export class AddressUtils {
	/**
	 * 先去缓存中获取数据，如果缓存中不存在则去后端请求
	 * 将拿到的地址列表递给回调函数
	 * @param {Function} success 回调函数
	 */
	static getAll(success) {
		// 先去缓存中获取
		const address = uni.getStorageSync(key)
		if (address) {
			// 拿到了
			success(address)
		}
		else {
			// 没拿到
			AddressApi.getAll()
			.then(data => {
				// 放入缓存
				uni.setStorageSync(key, data)
				// 传给调用方
				success(data)
			})
		}
	}
	
	/**
	 * 创建一个地址，并将新创建的地址给予回调函数
	 * @param {Object} address	
	 * @param {Function} success
	 */
	static create(address, success) {
		this.getAll(addressList => {
			AddressApi.create(address)
			.then(data => {
				addressList.push(data)
				uni.setStorageSync(key, addressList)
				success(data)
			})
		})
	}
	
	/**
	 * 根据 ID 删除一个地址，并将地址列表通过回调函数返回
	 * @param {String} id
	 * @param {Function} success
	 */
	static deleteById(id, success) {
		this.getAll(addressList => {
			AddressApi.deleteById(id)
			.then(data => {
				addressList = addressList.filter(item => item.id != id)
				uni.setStorageSync(key, addressList)
				success(addressList)
			})
		})
	}
	
	/**
	 * 根据 ID 修改一个地址，并将地址列表通过回调函数返回
	 * @param {String} id
	 * @param {Object} address
	 * @param {Function} success
	 */
	static updateById(id, address, success) {
		this.getAll(addressList => {
			AddressApi.updateById(id, address)
			.then(data => {
				let index = addressList.findIndex(item => item.id == id)
				addressList[index] = data
				uni.setStorageSync(key, addressList)
				success(addressList)
			})
		})
	}
	
	/**
	 * 根据 ID 设置一个地址为默认地址，并将地址列表通过回调函数返回
	 * @param {String} id
	 * @param {Function} success
	 */
	static setDefault(id, success) {
		this.getAll(addressList => {
			AddressApi.setDefault(id)
			.then(data => {
				addressList.forEach(item => item.isDefault = false)
				let index = addressList.findIndex(item => item.id == id)
				addressList[index].isDefault = true
				uni.setStorageSync(key, addressList)
				success(addressList)
			})
		})
	}

	static getDefault(success) {
		this.getAll(addressList => {
			let index = addressList.findIndex(item => item.isDefault == true)
			success(addressList[index])
		})
	}

	/**
	 * 重新加载数据
	 * @param {Function} success
	 */
	static reload(success) {
		AddressApi.getAll()
		.then(data => {
			// 放入缓存
			uni.setStorageSync(key, data)
			// 传给调用方
			success(data)
		})
	}

	static clear() {
		uni.removeStorageSync(key)
	}
}